package cn.fl.warrant.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tc.esign.facade.esign.api.IFilSignApiService;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.warrant.service.IArcApplyDetailService;
import cn.fl.warrant.service.IArcMgrMainService;
import cn.fl.warrant.transform.ArcApplyDetailVOTransform;
import cn.fl.warrant.vo.ArcApplyDetailVO;
import cn.fl.warrant.vo.ArcMgrDetailVO;
import fl.warrant.facade.archive.api.IArcApplyDetailApiService;
import fl.warrant.facade.archive.condition.ArcApplyDetailQC;
import fl.warrant.facade.archive.dto.ArcApplyDetailDTO;

/**
 * @Description: 归档申请明细表(旧表：T_API_DOCUMENT_DOSSIERSTORAGE) 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class ArcApplyDetailServiceImpl implements IArcApplyDetailService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IArcApplyDetailService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IArcApplyDetailApiService arcApplyDetailApiService;
	@Autowired
	private IFilSignApiService filSignApiService;
	@Autowired
	private IArcMgrMainService arcMgrMainService;
	
	@Override
	public ArcApplyDetailVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		ArcApplyDetailDTO dto = this.arcApplyDetailApiService.selectById(id);
		return ArcApplyDetailVOTransform.toVO(dto);
	}

	@Override
	public Long add(ArcApplyDetailVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.arcApplyDetailApiService.add(ArcApplyDetailVOTransform.toDTO(entity));
		
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.arcApplyDetailApiService.removeById(id);
		
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(ArcApplyDetailVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.arcApplyDetailApiService.modify(ArcApplyDetailVOTransform.toDTO(entity));
		
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.arcApplyDetailApiService.searchListPage(page);
		
	}
	@Override
	public List<ArcApplyDetailVO> searchList(ArcApplyDetailQC qc ){
		String syscd=CurrentThreadContext.getCurrentSysCd();
		String orgcd=CurrentThreadContext.getCurrentOrgCd();
		List<ArcApplyDetailVO> list=ArcApplyDetailVOTransform.toVOList(this.arcApplyDetailApiService.searchList(qc, syscd, orgcd));
		List<ArcMgrDetailVO> list1=new ArrayList<ArcMgrDetailVO>();
		for (ArcApplyDetailVO avo:list) {
			ArcMgrDetailVO vo =new ArcMgrDetailVO();
			vo.setContractNo(avo.getContractNo());
			vo.setPaylistCode(avo.getPaylistCode());
			vo.setDossierTemp(avo.getDossierTemp());
			vo.setEleId(avo.getEleId());
			vo.setId(avo.getId());
			vo.setFileName(avo.getFileName());
			vo.setFileFlag(avo.getFileFlag());
			list1.add(vo);
		}
		//合同电子赋值
		arcMgrMainService.getFilSign(list1);
		//获取附件
		arcMgrMainService.getFiles(list1);
		for (ArcMgrDetailVO vo:list1) {
			for (ArcApplyDetailVO avo:list) {
				if (avo.getId().equals(vo.getId())) {
					avo.setEleId(vo.getEleId());
					avo.setFilePath(vo.getFilePath());
					avo.setUlFiles(vo.getUlFiles());
					avo.setUlFlag(vo.getUlFlag());
					if (vo.getBz()!=null) {
						avo.setBz(vo.getBz());
					}					
					break;
				}
			}
		}
		return list;
	}
}
